package com.fast.user.web.controller.dept;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.fast.base.db.page.PageView;
import com.fast.base.db.page.QueryResult;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.data.MathUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.web.BaseController;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.dept.Company;
import com.fast.user.service.dept.CompanyService;

/**
 * ClassName:CompanyController <br/>
 * 用途:公司信息控制处理 <br/>
 * 开发人员：lushuifa http://lushuifa.iteye.com/ 邮箱:lsflu@126.com
 * 
 * @version
 * @since JDK 1.6
 * @see Date: 2014年12月17日 下午2:18:46 <br/>
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController{
	

	
	@Resource(name="companyService")
	private CompanyService beanService;
	
	/**
	 * 得到单位列表
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getComboboxData.do")
	public List<Company> getComboboxData() {
		List<Company> comList = this.beanService.queryForBeanList(Company.class,"");
		return comList;
	}

	/**
	 * 编辑[单位]信息
	 * @param Company
	 */
	@AnoSysLog(actionName="添加编辑企业信息")
	@ResponseBody
	@RequestMapping("/editCompany.do")
	public ResponseData editCompany() {
		ResponseData responseData = new ResponseData();
		AdminUser curAdminUser = AdminCacheUtils.getLoginAdminUser();
		Company company = null;
		try {
			int id = webUtil.getInt("id");
			if (id > 0) {
				company = this.beanService.queryForBeanById(Company.class, id);
				company = (Company) webUtil.bindDataToBean(company);
			} else {
				company = new Company();
				company = (Company) webUtil.bindDataToBean(company);
				company.setUuid(MathUtils.getTheOnlyStr());
				company.setCreater(curAdminUser.getUserName());
				company.setCreaterId(curAdminUser.getId());
				company.setCreateTime(new Date());
			}
			// 当存在UUID时的处理
			String parentUUID = company.getParentUUID();
			if(StringUtils.isNotBlank(parentUUID)){
				Company pCompany = this.beanService.queryForBeanByUUID(Company.class, parentUUID);
				company.setParentName(pCompany.getCompanyName());
			}else{
				company.setParentUUID("0");
				company.setParentName("根目录");
			}
			company.setEditorId(curAdminUser.getId());
			company.setEditorName(curAdminUser.getUserName());
			company.setEditTime(new Date());
			if (this.beanService.merge(company) != null) {
				responseData.setMsg("编辑编号为[" + company.getUuid() + "]操作成功");
			}
			responseData.setCode("00");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMsg("编辑编号为[" + company.getUuid() + "]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delCompany.do")
	@AnoSysLog(actionName="删除企业信息")
	public ResponseData delCompany(int id) {
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = beanService.deleteByIdForHS(Company.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.setMsg("删除编号为["+id+"]操作成功");
			}else{
				responseData.setMsg("删除编号为["+id+"]操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除编号为["+id+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除多个[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMoreCompany.do")
	@AnoSysLog(actionName="批量删除企业信息")
	public ResponseData delMoreCompany(String ids) {
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = beanService.deleteByIds(Company.class, ids);
			responseData.setCode("00");
			responseData.setMsg("删除[单位]编号为["+ids+"]操作成功,共成功["+actionCount+"]");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除[单位]["+ids+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除多个机构企业信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelCompanyForHS.do")
	@AnoSysLog(actionName="批量删除机构企业信息")
	public ResponseData batchDelCompanyForHS(String ids) {
		LogUtils.info("batchDelCompanyForHS is comming!");
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = this.beanService.batchDeleteByIdsForHS(Company.class, ids);
			if(!actionFlag){
				responseData.failue();
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.info("删除管理员异常batchDelCompanyForHS err",e);
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 得到[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJsonDetail.do")
	public ResponseData getJsonDetail(int id) {
		ResponseData responseData = new ResponseData();
		Company bean = beanService.queryForBeanById(Company.class, id);
		try {
			responseData.setCode("00");
			responseData.setData(bean);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 得到全部[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJsonAllList.do")
	public ResponseData getCompanyAllList() {
		ResponseData responseData = new ResponseData();
		try {
			JSONArray jsonArray = beanService.getCompanyTree("0");
			List<Map<String,Object>> beanList = beanService.zzCompanList(jsonArray,-1);
			responseData.setCode("00");
			responseData.setData(beanList);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 标题：getCompanyList4Tree <br/>
	 * 作用： 得到单位列表以树型结构展示    <br/>
	 * @author developer-lushuifa
	 * @since JDK 1.6
	 */
	@ResponseBody
	@RequestMapping("/getCompanyList4Tree.do")
	public JSONArray getCompanyList4Tree(String parentUUID){
		JSONArray topArr = this.beanService.getCompanyTree(parentUUID);
		return topArr;
	}
	
	/**
	 * 删除[单位]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCompanyDetail.do")
	public String getCompanyDetail(int id) {
		Company bean = beanService.queryForBeanById(Company.class, id);
		try {
			webUtil.setAttr("company", bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/admin/companyDetail";
	}
	
	/**
	 * 得到[单位]列表
	 * @param dtGridPager Pager对象
	 * @param request 请求对象
	 * @param response 响应对象
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/getCompanyList.do", method=RequestMethod.POST)
	public Map<String,Object> getCompanyList(String companyName,String parentUUID){
		Map<String,Object> gridMap = new HashMap<String,Object>();
		try {
			int page = webUtil.getInt("page");//当前页
			int pagesize = webUtil.getInt("rows");//每页面显示多少条
			StringBuffer sb = new StringBuffer(" where 1=1 ");
			if(StringUtils.isNotEmpty(parentUUID)){
				sb.append("and parentUUID='"+parentUUID+"'");
			}
			if(StringUtils.isNotEmpty(companyName)){
				sb.append("and companyName like '%"+companyName+"%' ");
			}
			sb.append("and delete_flag=0");
			String ordersql = " order by orderNumber asc ";
			QueryResult<Company>  queryResult = this.beanService.queryForBeanListByPage(Company.class, sb.toString(), ordersql,(page-1)*pagesize,pagesize);
			PageView<Company> pageView = new PageView<Company>();
			pageView.setCurrentPage(page);
			pageView.setQueryResult(queryResult);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
 		return gridMap;
	}
}
